package 
{
	import away3d.containers.ObjectContainer3D;
	import away3d.containers.View3D;
	import away3d.core.clip.RectangleClipping;
	import away3d.events.MouseEvent3D;
	import away3d.materials.BitmapFileMaterial;
	import away3d.primitives.Plane;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	import gs.easing.Quint;
	
	import mx.containers.ControlBar;
	import mx.containers.Panel;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.Spacer;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	import spark.components.Image;
	import widok.Window;
	
	

	[SWF(frameRate="62", backgroundColor="#FFFF00")]
	
	public class karuzela extends UIComponent
	{
		protected var planes:Array = [];
		protected var numItems:Number = 17;
		protected var radius:Number = 850;
		protected var currentItem:Number = 0;
		
		protected var bitmapMaterial:BitmapFileMaterial;
		protected var planesHolder:ObjectContainer3D;
		protected var rightBtn:Sprite;
		protected var leftBtn:Sprite;
		protected var view2:View3D;
		
		public var lolol:int;
		
		
		public function karuzela():void 
		{
			super();
			
			
			this.addEventListener(Event.ENTER_FRAME,startRendering);
			
		}
		
	
		
		
		
		override protected function createChildren():void
		{
			super.createChildren();
			this.createPlanes();
			
			arrangePlanes();
			adjustCamera();
			
		}
		
		
		protected function createPlanes():void 
		{
		
			view2 = new View3D();
			addChild(view2);
			
			planesHolder = new ObjectContainer3D();
			
			
			
			
			for (var i:Number = 0; i <17; i++) 
			{
				bitmapMaterial = new BitmapFileMaterial("images/image"+i+".jpg");
				bitmapMaterial.smooth = true;
				
				
				var plane:Plane = new Plane({material:bitmapMaterial, width:150, height:234});
				plane.name="image"+i;
				plane.addEventListener(MouseEvent3D.MOUSE_DOWN,objectClick);
				planes.push(plane);
				
			
				planesHolder.addChild(plane);
			}
			
			
			
			
			view2.scene.addChild(planesHolder);
		}
		
		private function objectClick(e:MouseEvent3D):void
		{
			
			var win : Window = new Window();
			trace("images/"+ e.target.name+".jpg")
			var temp:mx.controls.Image=new mx.controls.Image();
			temp.source="images/"+ e.target.name+".jpg"
	
			win.width=temp.contentWidth
			win.height=temp.contentHeight;
		
			win.addChild(temp);
			win.x=this.width/2;
			win.y=this.height/2;
			PopUpManager.addPopUp(win,this,true);
			
			
			
		}
		
		
		 //based on http://www.gotoandlearn.com/play.php?id=32
		//http://www.flash-3d.net/2010/11/a-simple-away3d-carousel/
		
		protected function arrangePlanes():void 
		{ 
			//Set properties of our planes
			for (var i:int = 0; i < planes.length; i++) 
			{
				var angle:Number = Math.PI * 2 / numItems * i;
				var plane:Plane = planes[i];
				plane.yUp = false;
				plane.bothsides = true;
				plane.x = Math.cos(angle) * radius;
				plane.z = Math.sin(angle) * radius;
				plane.rotationY = -360 / numItems * i - 90;
			}
			
		
			rotate(5);
		}
		
		protected function adjustCamera():void 
		{
			
			view2.camera.zoom = 10;
			view2.camera.focus = 40;
			view2.camera.y = 50;
			view2.camera.z =-1500;
		}
		
		
		public function rotate(ITEM:int):void 
		{
			var rotateTo:Number = (360 / numItems) * (ITEM)+90;
			TweenLite.to(planesHolder, 1, { rotationY:rotateTo, ease:Quint.easeInOut } );
		}
		
		protected function startRendering(event:Event):void 
		{
			
			view2.render();
			
			
		}
		
		
		
		
		public function buttonClick(kierunek:Boolean):void 
		{
			switch (kierunek)
			{
				case true:
				{
					
					rotate(++currentItem );
					break;
					
				}
				case false:
				{
					
				
					
					rotate(--currentItem);
					break;
				}
			}
			
		}
		
		
		
		[Bindable]
		public var gdzie:int;
		
		
		
		
		
		
		
		
		
		
		override protected function updateDisplayList(
			unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if(this.width / 2 != this.view2.x)
				this.view2.x = this.width / 2;
			
			view2.clipping = new RectangleClipping({minX:-unscaledWidth/2,minY:-unscaledHeight/2,maxX:unscaledWidth/2,maxY:unscaledHeight/2});
			
			
			if(this.height / 2 != this.view2.y)
				this.view2.y = this.height / 2;
			
		}
		
		
		
		
	}
}